﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.DAL.UserCenter;
using Kovan.Model.Entity.UserCenter;
using Kovan.Library.IO;
using Kovan.Model.ViewModel.UserCenter;
using System.Xml;

namespace Kovan.BLL.UserCenter
{
    public class BActAuthority
    {
        DActAuthority dal = new DActAuthority();

        #region 当前类的实例
        private static readonly BActAuthority Instances = new BActAuthority();

        private BActAuthority() { }

        public static BActAuthority GetInstances()
        {
            return Instances;
        }
        #endregion

        /// <summary>
        /// 根据模块ID获取列表
        /// </summary>
        /// <param name="moduleid"></param>
        /// <returns></returns>
        public IEnumerable<act_authority> GetListByModuleId(long moduleid)
        {
            return dal.GetListByModuleId(moduleid);
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public act_authority GetModel(int id)
        {
            return dal.GetModel(id);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public int Add(act_authority model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="moduleid">模块ID</param>
        /// <param name="codes">权限集合（模块ID未赋值）</param>
        /// <returns></returns>
        public int BatchAdd(long moduleid, IEnumerable<string> codes)
        {
            var authoritys = new List<act_authority>();
            var AuthorityCodes = BActAuthority.GetInstances().GetAuthorityCode();
            foreach (var code in codes)
            {
                var ac = AuthorityCodes.FirstOrDefault(c => c.code == code);
                var model = new act_authority();
                model.code = code;
                model.code_order = int.Parse(ac.code_order);
                model.name = ac.name;
                authoritys.Add(model);
            }
            return dal.BatchAdd(moduleid, authoritys);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public int Delete(int id)
        {
            return dal.Delete(id);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="moduleid">模块ID</param>
        /// <returns></returns>
        public int DeleteByModuleId(long moduleid)
        {
            return dal.DeleteByModuleId(moduleid);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public int Update(act_authority model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 根据XML文件获取权限编码
        /// </summary>
        /// <returns></returns>
        public List<EAuthorityCode> GetAuthorityCode()
        {
            List<EAuthorityCode> list = new List<EAuthorityCode>();
            var path = PathUtility.GetPhysicalPath("~/UserData/Config/authority_code.xml");
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(path);
            XmlNodeList xnl = xdoc.SelectNodes("/AuthorityCode/Item");
            foreach (XmlNode node in xnl)
            {
                var item = new EAuthorityCode();
                item.code = node.Attributes["code"].Value.Trim();
                item.code_order = node.Attributes["order"].Value.Trim();
                item.name = node.InnerText.Trim();
                list.Add(item);
            }
            return list;
        }
    }
}
